<!DOCTYPE html>
<html>
<head>
    {{template "header"}}
</head>
<body>
<script>
    let tableid = webix.uid()
    let getColumns = function () {
        return [
            {
                height: 30,
                cols: [
                    {view: "text", name: "name", label: gtr("Name"), css: "nborder-input",},
                    {
                        view: "combo", name: "node_id", label: tr("radius","Node"),
                        options: "/admin/node/options",
                        css: "nborder-input",
                    },
                ]
            },
            {
                height: 30,
                cols: [
                    {view: "text", name: "addr_pool", label: tr("radius","Addr pool"), css: "nborder-input",},
                    {view: "text", name: "active_num", label: tr("radius","Active num"), css: "nborder-input",},
                ]
            },
            {
                height: 30,
                cols: [
                    {view: "text", name: "up_rate", label: tr("radius","UpRate (kbps)"), css: "nborder-input",},
                    {view: "text", name: "down_rate", label: tr("radius","DownRate (kbps)"), css: "nborder-input",},
                ]
            },
            {
                height: 30,
                cols: [
                    {
                        view: "combo",
                        name: "status",
                        label: tr("radius","Status"),
                        value: "enabled",
                        options: ["enabled","disabled"],
                        css: "nborder-input",
                    },
                ]
            },
            {name: "remark", view: "codemirror-editor", mode: "markdown"},
        ]
    }

    let openDetail = function (item) {
        let winid = "radius.profile.detail." + item.id
        wxui.openWindow({
            width: 720,
            height: 576,
            winid: winid,
            title: tr("radius","Radius profile detail"),
            body: {
                view: "form",
                paddingX: 20,
                scroll: "auto",
                elementsConfig: {
                    marginY: 0,
                    labelWidth: 120,
                },
                css: "detail-form",
                url: '/admin/radius/profile/get?id=' + item.id,
                elements: [
                    {
                        height: 30,
                        cols: [
                            {
                                view: "text",
                                name: "name",
                                label: gtr("Name"),
                                readonly: true,
                                css: "nborder-input",
                            },
                            {
                                view: "combo",
                                name: "status",
                                label: gtr("Status"),
                                readonly: true,
                                options: ["enabled", "disabled"],
                                css: "nborder-input",
                            }
                        ]
                    },
                    {
                        height: 30,
                        cols: [
                            {
                                view: "text",
                                name: "addr_pool",
                                readonly: true,
                                label: tr("radius","Addr pool"),
                                css: "nborder-input",
                            },
                            {
                                view: "text",
                                name: "active_num",
                                readonly: true,
                                label: tr("radius","Active num"),
                                css: "nborder-input",
                            },
                        ]
                    },
                    {
                        height: 30,
                        cols: [
                            {
                                view: "text",
                                name: "up_rate",
                                readonly: true,
                                label: tr("radius","UpRate (kbps)"),
                                css: "nborder-input",
                            },
                            {
                                view: "text",
                                name: "down_rate",
                                readonly: true,
                                label: tr("radius","DownRate (kbps)"),
                                css: "nborder-input",
                            },
                        ]
                    },
                    {name: "remark", readonly: true, view: "codemirror-editor", value: item.remark, mode: "markdown"},
                ],
            }
        }).show()
    }

    let deleteItem = function (ids, callback) {
        webix.confirm({
            title: gtr("Operation confirmation"),
            ok: gtr("Yes"), cancel: gtr("No"),
            text: gtr("Confirm to delete? This operation is irreversible."),
            callback: function (ev) {
                if (ev) {
                    webix.ajax().get('/admin/radius/profile/delete', {ids: ids}).then(function (result) {
                        let resp = result.json();
                        webix.message({type: resp.msgtype, text: resp.msg, expire: 2000});
                        if (callback)
                            callback()
                    }).fail(function (xhr) {
                        webix.message({type: 'error', text: gtr("Delete Failure:") + xhr.statusText, expire: 2000});
                    });
                }
            }
        });
    }

    webix.ready(function () {
        let importUrl = '/admin/radius/profile/import'
        let exportUrl = '/admin/radius/profile/export'
        let tableid = webix.uid();
        let uploadid = webix.uid();
        let queryid = webix.uid()
        let reloadData = wxui.reloadDataFunc(tableid, "/admin/radius/profile/query", queryid)
        wxui.initUploadApi(uploadid, importUrl, reloadData);

        webix.ui({
            css: "main-panel",
            padding: 7,
            rows: [
                wxui.getPageToolbar({
                    title:  tr("radius","Radius profile"),
                    icon: "mdi mdi-application-cog",
                    elements: [
                        wxui.getPrimaryButton(gtr("Edit"), 90, false, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                vitem.api_pwd = ""
                                wxui.openFormWindow({
                                    width: 640,
                                    height: 640,
                                    title: tr("radius","Edit radius profile"),
                                    data: vitem,
                                    post: "/admin/radius/profile/update",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Clone"), 90, false, function () {
                            let item = $$(tableid).getSelectedItem();
                            if (item) {
                                let vitem = webix.copy(item)
                                vitem.id = ""
                                vitem.api_pwd = ""
                                wxui.openFormWindow({
                                    width: 640,
                                    height: 640,
                                    title: tr("radius","Clone radius profile"),
                                    data: vitem,
                                    post: "/admin/radius/profile/add",
                                    callback: reloadData,
                                    elements: getColumns()
                                }).show();
                            } else {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            }
                        }),
                        wxui.getPrimaryButton(gtr("Create"), 90, false, function () {
                            wxui.openFormWindow({
                                width: 640,
                                height: 640,
                                title: tr("radius","Create radius profile"),
                                post: "/admin/radius/profile/add",
                                callback: reloadData,
                                elements: getColumns()
                            }).show();
                        }),
                        wxui.getDangerButton(gtr("Remove"), 90, false, function () {
                            let rows = wxui.getTableCheckedIds(tableid);
                            if (rows.length === 0) {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            } else {
                                deleteItem(rows.join(","), reloadData);
                            }
                        }),
                    ],
                }),
                wxui.getTableQueryCustomForm(queryid, [
                    {
                        cols: [
                            {view: "search", id: "keyword", name: "keyword", placeholder: "keywords", width: 320},
                            {
                                view: "button",
                                label: gtr("Query"),
                                css: "webix_transparent",
                                type: "icon",
                                icon: "mdi mdi-search-web",
                                borderless: true,
                                width: 100,
                                click: function () {
                                    reloadData()
                                }
                            }, {}
                        ]
                    }
                ]),
                wxui.getDatatable({
                    tableid: tableid,
                    url: '/admin/radius/profile/query',
                    columns: [
                        {
                            id: "state",
                            header: {content: "masterCheckbox", css: "center"},
                            headermenu: false,
                            css: "center", width:45,
                            template: "{common.checkbox()}"
                        },
                        {
                            id: "node_id", options: "/admin/node/options", adjust: true, sort: "server",
                            header: [tr("radius","Node")],
                        },
                        {
                            id: "name",
                            header: [gtr("Name")],
                            adjust: true,
                            sort: "server",
                            template: "<a class='do_detail' href='javascript:void(0)'>#name#</a>"
                        },
                        {
                            id: "status",
                            header: [gtr("Status")],
                            adjust: true,
                            sort: "server"
                        },
                        {
                            id: "addr_pool",
                            header: [tr("radius","Addr pool")],
                            adjust: true,
                            sort: "server"
                        },
                        {
                            id: "active_num",
                            header: [tr("radius","Active num")],
                            adjust: true,
                            sort: "server"
                        },
                        {
                            id: "up_rate", header: [tr("radius","UpRate (Mbps)")], template: function (obj) {
                                return Number(obj.up_rate) / 1000
                            }, adjust: true, sort: "server",
                        },
                        {
                            id: "down_rate",
                            header: [tr("radius","DownRate (Mbps)")],
                            template: function (obj) {
                                return Number(obj.up_rate) / 1000
                            },
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "remark",
                            header: [gtr("Remark")],
                            fillspace: true,
                            sort: "server"
                        },
                        // {header: {content: "headerMenu"}, headermenu: false}
                    ],
                    leftSplit: 1,
                    pager: true,
                    on: {
                        onItemDblClick: function (id, e, node) {
                            openDetail(this.getItem(id))
                        }
                    },
                    onClick: {
                        do_detail: function (e, id) {
                            openDetail(this.getItem(id))
                        }
                    },
                }),
                wxui.getTableFooterBar({
                    tableid: tableid,
                    actions: [
                        wxui.getIconButton(gtr("Import"), 100, "import", false, function () {
                            $$(uploadid).fileDialog({});
                        }),
                        wxui.getIconButton(gtr("Export"), 100, "download", false, function () {
                            wxui.exportData(exportUrl, 'RadiusProfiles.csv')
                        }),
                    ],
                    callback: reloadData
                }),
            ]
        })
    })
</script>
</body>
</html>